package com.colorful.insuranceproject.commonActivity;

import android.content.Context;
import android.os.SystemClock;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.apush.APushProcessor;
import com.colorful.insuranceproject.apush.MyMessageReceiver;
import com.colorful.insuranceproject.bean.ThirdUser;
import com.colorful.insuranceproject.consts.CityDataHolder;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.consts.URLConst;
import com.colorful.insuranceproject.request.Lander;
import com.lovely3x.common.CommonApplication;
import com.lovely3x.common.managements.CityManager;
import com.lovely3x.common.managements.LocationManager;
import com.lovely3x.common.managements.pay.PaymentManager;
import com.lovely3x.common.managements.user.IUser;
import com.lovely3x.common.managements.user.TokenManager;
import com.lovely3x.common.managements.user.UserChangedListener;
import com.lovely3x.common.managements.user.UserLoginStateChangedListener;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.common.requests.BaseCodeTable;
import com.lovely3x.common.requests.BaseURLConst;
import com.lovely3x.common.utils.ALog;
import com.lovely3x.common.utils.CommonUtils;
import com.lovely3x.loginandresgiter.bean.SimpleUser;
import com.lovely3x.paymentlibrary.alipay.AliPay2Action;
import com.lovely3x.paymentlibrary.alipay.AliPay2ServerSignAction;
import com.lovely3x.paymentlibrary.lianlian.LianLianPayAction;
import com.lovely3x.paymentlibrary.wechatpay.WeChatPayAction;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import static com.colorful.insuranceproject.BuildConfig.BUGLY_APP_KEY;

/**
 * Created by handsome-Bob on 2017/8/15.
 * <p>
 * application
 */
public class InsuranceApplication extends CommonApplication implements UserChangedListener {

    private static final String TAG = "ColorFulApplication";

    @Override
    public void onCreate() {
        super.onCreate();
        CrashReport.initCrashReport(getApplicationContext(), BUGLY_APP_KEY, ALog.DEBUG);
        long startTime = SystemClock.elapsedRealtime();
        long interval = SystemClock.elapsedRealtime() - startTime;
        ALog.i(TAG, "Application launching consuming time " + interval + " [ms]");

        /*
         *   初始化友盟统计
         */
        initUmenAnaylas();
        /*
         *   初始化阿里云推送
         */
        initCloudChannel(this);
    }

    /**
     * 初始化云推送通道
     */
    private void initCloudChannel(Context context) {
        PushServiceFactory.init(context);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(context, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                ALog.e(TAG, "init cloudchannel success");
            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                ALog.e(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
    }

    public void initSlowEvent() {
        super.initSlowEvent();

        //  初始化三方登陆信息
        initSocial();

        if (getPackageName().equals(CommonUtils.getProcessName(this))) {

            /*
             *  初始化推送
             */
            MyMessageReceiver.getInstance().init(this);

            /*
             *  化码表
             */
            BaseCodeTable.init(new CodeTable(this));

            /*
             *  初始化网络请求
             */
            BaseURLConst.init(new URLConst());

            /*
             *  Token管理
             */
            TokenManager.getInstance().init(this);

            /*
             *  初始化登录器
             */
            UserManager.getInstance().setLander(new Lander());

            /*
             *   初始化地图管理器
             */
            LocationManager.getInstance().init(this);

            /*
             *    初始化城市选择
             */
            CityManager.getInstance().init(this);

            /*
             * 初始化用户管理器
             */
            UserManager.getInstance().init(this);

            /*
             *  初始化注册用户变化监听器
             */
            UserManager.getInstance().addUserChangedListener(this);

            /*
             *   将自己注册到Jpush中去
             */
            APushProcessor.registerSelfToPushManager(this);

            /*
             * 初始化支付库
             */
            PaymentManager.getInstance().registerInitHook(AliPay2ServerSignAction.INSTANCE);
            PaymentManager.getInstance().registerInitHook(AliPay2Action.INSTANCE);
            PaymentManager.getInstance().registerInitHook(WeChatPayAction.INSTANCE);
            PaymentManager.getInstance().registerInitHook(LianLianPayAction.INSTANCE);
//            PaymentManager.getInstance().registerInitHook(UnionPayAction.INSTANCE);
            PaymentManager.getInstance().init(this);

            //加载城市选择数据
            CityDataHolder.load(this);
        }
    }

    /**
     * 当用户发生变化后执行
     * //     * @param preUser     上一个用户
     * //     * @param currentUser 当前用户
     */
    @Override
    public void onUserChanged(IUser preUser, IUser currentUser) {

    }

    private void initSocial() {

        UMShareAPI.get(this);
        //微信
        PlatformConfig.setWeixin(com.lovely3x.umeng.social.library.BuildConfig.WX_APP_KEY, com.lovely3x.umeng.social.library.BuildConfig.WX_SECRET);
        //新浪微博
        PlatformConfig.setSinaWeibo(com.lovely3x.umeng.social.library.BuildConfig.SINA_APP_KEY, com.lovely3x.umeng.social.library.BuildConfig.SINA_SECRET, URLConst.WE_CHAT_CALL_BACK_URL);
        //qq空间
        PlatformConfig.setQQZone(com.lovely3x.umeng.social.library.BuildConfig.QQ_APP_ID, com.lovely3x.umeng.social.library.BuildConfig.QQ_APP_KEY);
    }

    /**
     * 初始化友盟统计
     */
    private void initUmenAnaylas() {
        MobclickAgent.setDebugMode(ALog.DEBUG);

        //设置友盟统计的类型
        MobclickAgent.setScenarioType(getApplicationContext(), MobclickAgent.EScenarioType.E_UM_NORMAL);

        //登录事件统计
        UserManager.getInstance().addUserStateChangedListener(new UserLoginStateChangedListener.SimpleUserLoginStateChangedListener() {
            @Override
            public void onUserLoginSuccessful(IUser IUser) {
                if (IUser instanceof ThirdUser) {
                    MobclickAgent.onProfileSignIn(((ThirdUser) IUser).typeToString(), String.valueOf(((SimpleUser) IUser).getPhone()));
                } else if (IUser instanceof SimpleUser) {
                    MobclickAgent.onProfileSignIn(String.valueOf(((SimpleUser) IUser).getPhone()));
                }
            }

            @Override
            public void onUserLogoutSuccessful(IUser IUser) {
                MobclickAgent.onProfileSignOff();
            }
        });
    }

    /**
     * 设置图片未加载的默认状态
     * 方
     */
    @Override
    public int getLoadingFailureResId() {
        return R.drawable.long_load;
    }

    @Override
    public int getEmptyURIImgResId() {
        return R.drawable.long_load_failure;
    }

    @Override
    public int getLoadingImgResId() {
        return R.drawable.long_load;
    }

    /**
     * 设置图片未加载的默认状态
     * 长
     */
    @Override
    public int getRectangleEmptyURIImageResId() {
        return R.drawable.long_load;
    }

    @Override
    public int getRectangleLoadingFailureResId() {
        return R.drawable.long_load_failure;
    }

    @Override
    public int getRectangleResIdLoadingImgResId() {
        return R.drawable.long_load;
    }
}
